//通过在线压缩工具对代码.css进行压缩，再复制到.min.css 
//https://tool.oschina.net/jscompress
//读取l流，转换流，写入流
//the streaming build system 基于流的构建系统

const fs = require('fs')
const { Transform } = require('stream')

exports.default = ()=>{
  //文件读取流
  const read = fs.createReadStream('normalize.css')

  //文件写入流
  const write = fs.createWriteStream('normalize.min.css')
  
  //文件转换流
  const transform = new Transform({ //
    transform:(chunk,encoding,callback)=>{ //核心转换过程
      //核心转换过程实现
      //chunk => 读取流中读到的内容（Buffer）toString转化程字符串
      const input = chunk.toString() 
      const output = input.replace(/\s+/g,'').replace(/\/\*.+?\*\//g,'')
      callback(null,output) //错误优先，没有错误传null
    }
  })

  //把读取出来的文件流导入写入文件流
  read
  .pipe(transform)
  .pipe(write)

  return read
}